//
//  WLNetworkingTools.swift
//  WLWeibo
//
//  Created by Devil on 16/8/13.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit
import AFNetworking

enum WLNetworkingToolsMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class WLNetworkingTools: AFHTTPSessionManager {

    static let sharedTools: WLNetworkingTools = {
    
        let tools = WLNetworkingTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")

        return tools
    
    }()
    
    func request(mothod: WLNetworkingToolsMethod,urlString:String,parameters:AnyObject?,success:(response: AnyObject?)->(),failure:(error: NSError) -> ()){
    
        if mothod == .GET{
        
            GET(urlString,parameters:parameters,progress: nil,success:{(_,response) -> Void in
            
                success(response: response)
            
                },failure:{(_,error) -> Void in
                    failure(error:error)
            
            })
        
        }else{
        
            POST(urlString,parameters:parameters,progress: nil,success:{(_,response) -> Void in
                
                success(response: response)
                
                },failure:{(_,error) -> Void in
                    failure(error:error)
                    
            })
            
        }
    
    }
   
}

extension WLNetworkingTools{

    func homeLoadData(sinceId:Int64,maxId:Int64, success:(response: AnyObject?)->(),failure:(error:NSError)->()){
    
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = [
            "access_token":WLUserAccountViewModel.sharedTools.userAccount!.access_token!,
            "sinceId":"\(sinceId)",
            "maxId":"\(maxId)"
        
        
        ]
        
        request(WLNetworkingToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    
    }

}

extension WLNetworkingTools{

    func oauthLoadUserAccount(code:String,success:(response:AnyObject?)-> (),failure:(error: NSError)->()){
    
        let urlString = "https://api.weibo.com/oauth2/access_token"

        let params = [
        
            "client_id":WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        
        ]
        request(WLNetworkingToolsMethod.POST,urlString: urlString,parameters: params
        ,success: success,failure: failure)
    
    }
    
    func oauthLoadUserInfo(userAccount: WLuserAccountModel,success:(response: AnyObject?)->(),failure:(error:NSError)->()){
    
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
        
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        
        ]
        print(params)
        
        request(WLNetworkingToolsMethod.GET,urlString: urlString,parameters: params,success: success,failure: failure)
     
    }

}

extension WLNetworkingTools{

    func getWeatherData(success: (response: AnyObject?)->(),failure:(error: NSError)->()){
    
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(WLNetworkingToolsMethod.GET,urlString:urlString,parameters: nil,success: success,failure:failure)
     }

}





